package com.schoolET.service.admin.adminImpl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolET.config.OssConfig;
import com.schoolET.entity.AdminAndRole;
import com.schoolET.entity.Administrators;
import com.schoolET.entity.dto.AdminDTO;
import com.schoolET.entity.dto.AdminListDTO;
import com.schoolET.entity.dto.FileUploadDTO;
import com.schoolET.entity.vo.AdminSelectVO;
import com.schoolET.error.costom.AddAdminException;
import com.schoolET.error.costom.AdminEditException;
import com.schoolET.mapper.AdminAndRoleMapper;
import com.schoolET.mapper.AdministratorsMapper;
import com.schoolET.mapper.RegionalAdminMapper;
import com.schoolET.service.admin.RegionAdminService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


@Service
public class RegionAdminServiceImpl extends ServiceImpl<AdministratorsMapper, Administrators> implements RegionAdminService {

    @Resource
    private RegionalAdminMapper regionAdminMapper;

    @Resource
    private OssConfig ossConfig;

    @Resource
    private AdminAndRoleMapper adminAndRoleMapper;

    @Override
    public List<Administrators> getAdminLists() {
        return regionAdminMapper.selectAllAdmin();
    }

    @Override
    public Page<AdminListDTO> getAdminList(AdminSelectVO adminSelectVO) {
        if (adminSelectVO.getPageNum() == null || adminSelectVO.getPageSize() == null) {
            adminSelectVO.setPageNum(1);
            adminSelectVO.setPageSize(5);
        }
        Page<AdminListDTO> page = new Page<>(adminSelectVO.getPageNum(),adminSelectVO.getPageSize());

        return regionAdminMapper.selectAllAdminByNameAndPosition(page, adminSelectVO);
    }

    @Override
    @Transactional
    public FileUploadDTO addAdmin(Administrators administrators, MultipartFile file) throws IOException {
        if (!regionAdminMapper.selectAdminByAccountAndName(administrators).isEmpty()) {
            throw new AddAdminException();
        }
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String salt = IdUtil.simpleUUID().substring(0, 20);
        md5.setSalt(salt.getBytes());
        String md5Pass = md5.digestHex(administrators.getPassword());
        administrators.setPassword(md5Pass);
        administrators.setSalt(salt);
        System.out.println(file.getOriginalFilename());
        String path = ossConfig.putObject(file, "avatar");
        administrators.setAvatar(path);
        String url = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+path;
        baseMapper.insert(administrators);
        Integer id = administrators.getId();
        AdminAndRole adminAndRole = new AdminAndRole();
        adminAndRole.setAdminId(id);
        adminAndRole.setRoleId(2);
        adminAndRoleMapper.insert(adminAndRole);
        return new FileUploadDTO(url,path);
    }

    @Override
    public List<Administrators> checkAdmin(Administrators administrators) {
        return regionAdminMapper.selectAdminByAll(administrators);
    }

    @Override
    public boolean deleteAdmin(Integer id) {
        baseMapper.deleteById(id);
        return true;
    }

    @Override
    @Transactional
    public FileUploadDTO updateAdmin(Administrators administrators, MultipartFile file) throws IOException {
        if (!regionAdminMapper.selectAdminByAll(administrators).isEmpty()) {
            throw new AdminEditException();
        }
        Administrators administrators1 = baseMapper.selectById(administrators.getId());
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String salt = IdUtil.simpleUUID().substring(0, 20);
        md5.setSalt(salt.getBytes());
        String md5Pass = md5.digestHex(administrators.getPassword());
        administrators.setPassword(md5Pass);
        administrators.setSalt(salt);
        System.out.println(file.getOriginalFilename());
        String path = ossConfig.putObject(file, "avatar");
        administrators.setAvatar(path);
        String url = "https://"+ossConfig.getBucketName()+"."+ossConfig.getEndpoint()+"/"+path;
        baseMapper.updateById(administrators);
        ossConfig.getOSSClient().deleteObject(ossConfig.getBucketName(), administrators1.getAvatar());
        return new FileUploadDTO(url, path);
    }

    @Override
    public AdminDTO getAdmin(Integer id) {
        Administrators administrators = baseMapper.selectById(id);
        if (administrators == null) {
            throw new AdminEditException();
        }
        AdminDTO adminDTO = new AdminDTO();
        BeanUtil.copyProperties(administrators, adminDTO);
        //hutool数据脱敏
        adminDTO.setTelephone(DesensitizedUtil.mobilePhone(adminDTO.getTelephone()));
        return adminDTO;
    }

}
